package gao.xaiolei.dao;

import java.util.List;

import gao.xaiolei.enity.TaskReceiver;
import gao.xaiolei.enity.TaskReceiverId;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.persistence.MappedSuperclass;

@Repository("ITaskReceiverDao")
public interface ITaskReceiverDao extends JpaRepository<TaskReceiver, TaskReceiverId> {

	// 根据任务Id删除任务接收人
	@Modifying // update或者delete操作，在更新或者删除操作时，此注解必须加
	@Query(value = "delete TaskReceiver where id.TR_TASK_ID = :TR_TASK_ID")
	public void deleteByTaskId(@Param("TR_TASK_ID") String TR_TASK_ID);

	// 根据接收人Id删除任务接收人
	@Modifying // update或者delete操作，在更新或者删除操作时，此注解必须加
	@Query(value = "delete TaskReceiver where id.TR_USER_ID = :TR_USER_ID")
	public void deleteByUserId(@Param("TR_USER_ID") String TR_USER_ID);

	// 根据任务Id获取任务接收人信息
	@Query("select id.TR_USER_ID from TaskReceiver where id.TR_TASK_ID=:TR_TASK_ID")
	public List<String> queryByTaskId(@Param("TR_TASK_ID") String TR_TASK_ID);

	// 根据任务接收人Id获取任务信息
	@Query("select id.TR_TASK_ID from TaskReceiver where id.TR_USER_ID=:TR_USER_ID")
	public List<String> queryByUserId(@Param("TR_USER_ID") String TR_USER_ID);

	// 获取任务所有接收人状态
	@Query("select status from TaskReceiver where id.TR_TASK_ID=:TR_TASK_ID")
	public List<Integer> getTaskAllReceiveStatus(@Param("TR_TASK_ID") String TR_TASK_ID);

	// 获取某个人未读的任务数量
	@Query("select count(*) from TaskReceiver where id.TR_USER_ID=:receiver and status=:status")
	public Integer getCountReceiverNoread(@Param("receiver") String receiver,
												 @Param("status") Integer status);

	// 获取某个人与任务的状态未
	@Modifying
	@Query("update TaskReceiver set status=:status where id.TR_USER_ID=:receiver and id.TR_TASK_ID=:taskId")
	public void updateStatus(@Param("receiver") String receiver,
							 @Param("taskId") String taskId,
										  @Param("status") Integer status);
}
